package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.whmnx.base.bean.BaseBean;
import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.ui.fragment.FabuShouGouFragment;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * author : huang
 * date   : 2020/10/10 0010 14:55
 * version: 1.0
 * @author Administrator
 */
public class ShouGoModel extends ToolbarViewModel{
    private FabuShouGouFragment fragment;
    private int index;

    //交易数量
    public ObservableField<String> textNum  = new ObservableField<>("");

    public ObservableField<String> textPrice  = new ObservableField<>("");

    public ObservableField<String> textSprice  = new ObservableField<>("0.00");

    public ObservableField<String> etPrice  = new ObservableField<>();

    public ObservableField<String> etNum  = new ObservableField<>();

    public ObservableField<String> tvNum  = new ObservableField<>("收购播豆数量(个)");
    public ObservableField<String> tvPrice  = new ObservableField<>("收购播豆单价(元)");
    public ObservableField<String> btText  = new ObservableField<>("确认收购");


    public ShouGoModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(FabuShouGouFragment fabuShouGouFragment, int index) {
        this.fragment = fabuShouGouFragment;
        this.index = index;
        if (index == 1) {
            setTitleText("发布播豆收购");
            textNum.set("我拥有的播豆（个）：" + SPUtils.getInstance().getString(Constants.BEAN));
            textPrice.set("今日播豆均价（元）：" + SPUtils.getInstance().getString(Constants.AVERAGEPRICE));
            tvNum.set("收购播豆数量(个)");
            tvPrice.set("收购播豆单价(元)");
            btText.set("确认收购");

        }else {
            setTitleText("发布播豆出售");
            textNum.set("我拥有的播豆（个）：" + SPUtils.getInstance().getString(Constants.BEAN));
            textPrice.set("今日播豆均价（元）：" + SPUtils.getInstance().getString(Constants.AVERAGEPRICE));
            tvNum.set("出售播豆数量(个)");
            tvPrice.set("出售播豆单价(元)");
            btText.set("确认出售");
        }
       // textSprice.set(df.format((Double.valueOf(etPrice.get()) * Integer.parseInt(etNum.get()))) + "");
    }




    //底部按钮 //全部出售
    public BindingCommand OnClickCommand1 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            etNum.set(SPUtils.getInstance().getString(Constants.BEAN));
        }
    });

    //使用此价
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            etPrice.set(SPUtils.getInstance().getString(Constants.AVERAGEPRICE));
        }
    });
    //确定收购
    public BindingCommand OnClickCommand3 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (TextUtils.isEmpty(etNum.get())){
                ToastUtils.showShort("请输入播豆个数");
                return;
            }
            if (TextUtils.isEmpty(etPrice.get())){
                ToastUtils.showShort("请输入播豆单价");
                return;
            }
            Map<String, Object> params = new HashMap<>();
            params.put("deal_Num",etNum.get());
            params.put("deal_Price",etPrice.get());
            params.put("deal_Type",index - 1);
            params.put("userId",SPUtils.getInstance().getString(Constants.USERID));
            HttpClient.post(fragment.getActivity(), Constants.ADDDEALTOONE, params,new CallBack<BaseBean>() {
                @Override
                public void onSuccess(BaseBean result) {
                    if (index == 1){
                        ToastUtils.showShort("收购成功");
                    }else {
                        ToastUtils.showShort("出售成功");
                    }
                }
            });
        }
    });
}
